package com.zwz.controller;

import com.zwz.service.DatabaseMetaService;
import com.zwz.voObject.DatabaseMetaVO;
import com.zwz.voObject.DatabaseMetaVO.TableInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/database")
public class DatabaseMetaController {

    @Autowired
    private DatabaseMetaService databaseMetaService;

    @GetMapping("/tables")
    public DatabaseMetaVO getOneLevelQueryTagsOfCompanyInfoList() {
        log.info("获取所有表信息");
        try {
            List<TableInfo> tables = databaseMetaService.getAllTableInfo();
            return DatabaseMetaVO.success(tables);
        } catch (Exception e) {
            log.error("获取表信息失败", e);
            return DatabaseMetaVO.error("获取表信息失败：" + e.getMessage());
        }
    }

    @GetMapping("/tables/{tableName}")
    public DatabaseMetaVO getTwoLevelQueryTagsOfCompanyInfoList(@PathVariable String tableName) {
        log.info("获取表 {} 的详细信息", tableName);
        try {
            TableInfo tableInfo = databaseMetaService.getTableInfo(tableName);
            return DatabaseMetaVO.success(Arrays.asList(tableInfo));
        } catch (Exception e) {
            log.error("获取表信息失败", e);
            return DatabaseMetaVO.error("获取表信息失败：" + e.getMessage());
        }
    }
} 